﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Timers;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Threading;

namespace MINIGKP
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public static string IPorPortUrl = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\IPorPortMessage.txt";
        public string ddiD = "";
        public static bool ismake = false;
        public static string OrderNum1 = "";
        public static string OrderNum2 = "";
        public static int IsMakeInit1 = 0;
        public static int IsMakeInit2 = 0;
        public static int OrderId1 = 0;
        public static int OrderId2 = 0;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!File.Exists(IPorPortUrl))
                {
                    using (FileStream stream = File.Create(IPorPortUrl))
                    {
                        stream.Close();
                        stream.Dispose();
                        IPorPortInfo porPortInfo1 = IPorPortInfo.Instance;
                        File.WriteAllText(IPorPortUrl, JsonConvert.SerializeObject(porPortInfo1));
                    }
                }
                else
                {
                    var porPortInfo = JsonConvert.DeserializeObject<IPorPortInfo>(File.ReadAllText(IPorPortUrl));
                    IPorPortInfo.Instance.CoffeeIP = porPortInfo.CoffeeIP;
                    IPorPortInfo.Instance.CoffeePort = porPortInfo.CoffeePort;
                    IPorPortInfo.Instance.RobotIPLeft = porPortInfo.RobotIPLeft;
                    IPorPortInfo.Instance.EWMPortName = porPortInfo.EWMPortName;
                    IPorPortInfo.Instance.SMPortName = porPortInfo.SMPortName;
                    IPorPortInfo.Instance.IOPortName = porPortInfo.IOPortName;
                    IPorPortInfo.Instance.SweetInit1 = porPortInfo.SweetInit1;
                    IPorPortInfo.Instance.SweetInit2 = porPortInfo.SweetInit2;
                    IPorPortInfo.Instance.SweetInit3 = porPortInfo.SweetInit3;
                    IPorPortInfo.Instance.deviceID = porPortInfo.deviceID;
                    IPorPortInfo.Instance.url_start = porPortInfo.url_start;
                }
                Thread thr3 = new Thread(SocketClients.Instance.socketclient);
                thr3.IsBackground = true;
                thr3.Start();
                ShowName.Text = "";
                Thread thr1 = new Thread(MessageShow);
                thr1.IsBackground = true;
                thr1.Start();
                //System.Timers.Timer timer = new System.Timers.Timer();
                //timer.Interval = 2000;
                //timer.Elapsed += Timer_Elapsed;
                //timer.Start();
                this.Left = 1280;
                this.Top = 0;
            }
            catch (Exception)
            {
                return;
            }
        }
        /// <summary>
        /// 单个咖啡状态信息显示
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <param name="guige"></param>
        /// <param name="price"></param>
        /// <param name="img"></param>
        public void CoffeeTypeShow(int id, string name, int orderid)
        {
            try
            {
                this.Dispatcher.BeginInvoke(new Action(delegate
                {
                    //if (id == 1)
                    //{
                    //    CCode1.Text = orderid.ToString();
                    //    CName1.Text = name;
                    //    Ctext1.Text = IsMakeInit1 == 1 ? "Please wait while it is being processed." : "Please collect your meal at Exit A.";
                    //}
                    //else
                    //{
                    //    CCode2.Text = orderid.ToString();
                    //    CName2.Text = name;
                    //    Ctext2.Text = IsMakeInit2 == 1 ? "Please wait while it is being processed." : "Please collect your meal at Exit B.";

                    //}
                }));
                GC.Collect();
            }
            catch (Exception)
            {
                return;
            }
        }
        public void MessageShow()
        {
            try
            {
                while (true)
                {
                    try
                    {
                        Device device = new Device();
                        string lists = JsonConvert.SerializeObject(device);
                        var data = HttpMsg.Instance.deviceOrderQueueListMsg(lists);
                        if (data != null)
                        {
                            var msglist = JsonConvert.DeserializeObject<HTTPMsg>(data);
                            if (msglist != null)
                            {
                                if (msglist.data.ToString().Length > 10)
                                {
                                    var list = JsonConvert.DeserializeObject<List<deviceOrderQueueList>>(msglist.data.ToString());
                                    if (list.Count > 0)
                                    {
                                        DeviceOrderQueue ListMake1 = null;
                                        DeviceOrderQueue ListMake2 = null;
                                        if (MainWindow.IsMakeInit1 != 0 && OrderId1 != 0)
                                        {
                                            var list1 = list.FirstOrDefault(a => a.orderNum == OrderNum1);
                                            if (list1 != null)
                                                ListMake1 = list1.typeList2.FirstOrDefault(a => a.orderId == OrderId1);
                                        }
                                        if (MainWindow.IsMakeInit2 != 0 && OrderId2 != 0)
                                        {
                                            var list2 = list.FirstOrDefault(a => a.orderNum == OrderNum2);
                                            if (list2 != null)
                                                ListMake2 = list2.typeList2.FirstOrDefault(a => a.orderId == OrderId2);
                                        }
                                        this.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(delegate
                                        {
                                            string NameShow = "";
                                            if (ListMake1 != null)
                                                if (MainWindow.IsMakeInit1 == 1)
                                                    NameShow += OrderId1 + " 制作中";
                                                //CoffeeTypeShow(1, ListMake1.goodsName, ListMake1.orderId);
                                                else
                                                    NameShow += OrderId1 + " A口取餐";
                                            if (ListMake2 != null)
                                                if (MainWindow.IsMakeInit2 == 1)
                                                    if (ListMake1 != null)
                                                        NameShow += "         " + OrderId2 + " 制作中";
                                                    else
                                                        NameShow += OrderId2 + " 制作中";
                                                //CoffeeTypeShow(1, ListMake1.goodsName, ListMake1.orderId);
                                                else
                                                    NameShow += OrderId2 + " B口取餐";
                                            if (ListMake1 == null && ListMake2 == null)
                                            {
                                                this.Dispatcher.BeginInvoke(new Action(delegate
                                                {
                                                    ShowName.Text = "";
                                                }));
                                            }
                                            else
                                            {
                                                this.Dispatcher.BeginInvoke(new Action(delegate
                                                {
                                                    ShowName.Text = NameShow;
                                                }));
                                            }
                                        }));
                                    }
                                    else
                                    {
                                        this.Dispatcher.BeginInvoke(new Action(delegate
                                        {
                                            ShowName.Text = "";
                                        }));
                                    }
                                }
                                else
                                {
                                    this.Dispatcher.BeginInvoke(new Action(delegate
                                    {
                                        ShowName.Text = "";
                                    }));
                                }
                            }
                        }
                        else
                        {
                            this.Dispatcher.BeginInvoke(new Action(delegate
                            {
                                ShowName.Text = "";
                            }));
                        }
                    }
                    catch (Exception)
                    {
                    }
                    Thread.Sleep(2000);
                }
            }
            catch (Exception)
            {
                return;
            }
        }
        private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            try
            {

            }
            catch (Exception)
            {
            }
        }
        private void exit_MouseDown(object sender, MouseButtonEventArgs e)
        {
            try
            {
                if (e.ChangedButton == MouseButton.Left && e.ClickCount == 2)
                {
                    MessageBoxResult result = MessageBox.Show("Exit orders will not be made,Do you really want to quit?", "Exit", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
                    if (result == MessageBoxResult.Yes)
                    {
                        this.Dispatcher.BeginInvoke(new Action(delegate
                        {
                            System.Windows.Application.Current.Shutdown();
                        }));
                    }
                }
            }
            catch (Exception)
            {
                return;
            }
        }
    }
}
